using System;

namespace Nice157Advices.Advices
{
    /// <summary>
    /// 13、为类型输出格式化字符串
    /// </summary>
    public static class _13
    {
        /**
         * a.实现IFormattable接口可以根据用户的输入而格式化输出
         * b.使用格式化器，其好处就是不需要写在类里，跟类分离。
         */
        public static void Go ()
        {
            var person = new Person () { LastName = "Cao", FirstName = "Capdiem" };
            System.Console.WriteLine (person.ToString ());
            System.Console.WriteLine (person.ToString ("1", null));
            System.Console.WriteLine (person.ToString ("2", null));

            Student student = new Student { No = 1, Name = "Caoyouxian", Nickname = "capdiem" };
            System.Console.WriteLine (student.ToString ());
            var formatter = new StudentFomatter ();
            System.Console.WriteLine (formatter.Format ("1", student, null));
            System.Console.WriteLine (formatter.Format ("2", student, null));
            System.Console.WriteLine (formatter.Format ("3", student, null));
            System.Console.WriteLine (formatter.Format ("default", student, null));

            System.Console.WriteLine(formatter.Format(1, student, null));
        }

        private class Person : IFormattable
        {
            public string IDCode { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

            public override string ToString ()
            {
                return $"{this.LastName} {this.FirstName}";
            }

            // implementing IFormattable
            public string ToString (string format, IFormatProvider formatProvider)
            {
                switch (format)
                {
                    case "1":
                        return this.ToString ();
                    case "2":
                        return $"{this.FirstName} {this.LastName}";
                    default:
                        return this.ToString ();
                }
            }
        }

        private class Student
        {
            public int No { get; set; }
            public string Name { get; set; }
            public string Nickname { get; set; }
        }

        private class StudentFomatter : IFormatProvider, ICustomFormatter
        {
            public string Format (string format, object arg, IFormatProvider formatProvider)
            {
                Student student = arg as Student;
                if (student == null)
                {
                    return string.Empty;
                }
                switch (format)
                {
                    case "1":
                        return $"{student.No} {student.Name} {student.Nickname}";
                    case "2":
                        return $"{student.Name} {student.No} {student.Nickname}";
                    case "3":
                        return $"{student.No} {student.Nickname} {student.Name}";
                    default:
                        return $"{student.No} {student.Name} {student.Nickname}";
                }
            }

            public string Format (int format, object arg, IFormatProvider formatProvider)
            {
                return "HAHAHAH!";
            }

            public object GetFormat (Type formatType) =>
                formatType == typeof (ICustomFormatter) ? this : null;
        }
    }
}